// 2017/06/14 16:56:12
//var storage = require('/storage.js');
function Utils() {

  //构造函数
  function Utils() {
    return this
  }

  /**
   * app 小程序实例
   * timeStamp 点击事件时间
   */
  Utils.prototype.canTap = function (e) {
    var time = (new Date()).getTime();
    console.log(time)
    console.log(getApp().globalData.timeStamp)
    if (getApp().globalData.timeStamp + 500 < time) {
      getApp().globalData.timeStamp = time;
      console.log(getApp().globalData.timeStamp)
      return true;
    } else {
      return false;
    }
  }

  Utils.prototype.formatTime = function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()


    return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':')
  }

  // 2017-06-14 16:56
  Utils.prototype.formatDate = function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()

    return [year, month, day].map(this.formatNumber).join('-') + ' ' + [hour, minute].map(this.formatNumber).join(':')
  }

  // 06-14 16:56
  Utils.prototype.formatDate01 = function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()

    return [month, day].map(this.formatNumber).join('-') + ' ' + [hour, minute].map(this.formatNumber).join(':')
  }

  // 2017-06-14
  Utils.prototype.formatDate02 = function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()

    return [year, month, day].map(this.formatNumber).join('-')
  }

  // 16:56
  Utils.prototype.formatDate03 = function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()

    return [hour, minute].map(this.formatNumber).join(':')
  }

  // 2017/12/05 12:03
  Utils.prototype.formatDate04 = function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()


    return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute].map(this.formatNumber).join(':')
  }

  // 2017/12/05 12:03:00
  Utils.prototype.formatDate05 = function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()


    return [year, month, day].map(this.formatNumber).join('/') + ' ' + [hour, minute, second].map(this.formatNumber).join(':')
  }

  // 2017/12/05
  Utils.prototype.formatDate06 = function (date) {
    var year = date.getFullYear()
    var month = date.getMonth() + 1
    var day = date.getDate()

    var hour = date.getHours()
    var minute = date.getMinutes()
    var second = date.getSeconds()


    return [year, month, day].map(this.formatNumber).join('/')
  }


  Utils.prototype.formatNumber = function (n) {
    n = n.toString()
    return n[1] ? n : '0' + n
  }

  // 倒计时
  Utils.prototype.countdown = function (that) {
    var second = that.data.second
    that.setData({
      showTime: second == 0 ? "重新获取" : second + "s重新获取"
    });
    if (second == 0) {
      if (that.data.size == 11) {
        that.setData({
          hasCode: true
        });
      }
      that.setData({
        second: 60
      });
      return;
    }
    var time = setTimeout(function () {
      that.setData({
        second: second - 1
      });
      countdown(that);
    }, 1000)
  }


  // 支付登录弹出框
  // Utils.prototype.payLoginDialog = function (that) {
  //   if (!storage.isLoginFlag()) {
  //     wx.showModal({
  //       title: "提示",
  //       content: "检测到您的帐号还未绑定手机号，请绑定手机号。",
  //       confirmText: "绑定手机",
  //       success: function (res) {
  //         if (res.confirm) {
  //           wx.navigateTo({
  //             url: "/pages/login/login?pageType=1003"
  //           })
  //         }
  //       }
  //     })
  //   } else {
  //     wx.navigateTo({
  //       url: "/pages/details/pay?id=" + that.data.speciesInfo.id,
  //     })
  //   }
  // }

  /**
   * 获取屏幕宽高
   */
  Utils.prototype.windowSize = function () {
    var windowSize = {};
    //获取屏幕宽高  
    wx.getSystemInfo({
      success: function (res) {
        var windowWidth = res.windowWidth;
        var windowHeight = res.windowHeight;
        //console.log(res)
        //console.log('windowWidth: ' + windowWidth)
        //console.log('windowHeight: ' + windowHeight)
        windowSize.winWidth = windowWidth;
        windowSize.winHeight = windowHeight;
      }
    })
    return windowSize;
  }

  /**
   * 商品优惠价计算，即减除多少钱，单位：分
   * 优惠券可抵金额
   * price：原价
   * coupon：优惠券对象
   */
  Utils.prototype.concessionalRate = function (that, price, coupon) {
    var amount = 0;
    if (coupon.type == "消费折扣") {
      if (price >= coupon.value1) {
        amount = price - Math.round(price * coupon.value2 / 1000)
      } else {
        that.showZanTopTips(2, '商品价格没有达到优惠券使用条件');
      }
    } else if (coupon.type == "消费满减") {
      if (price > coupon.value1) {
        amount = coupon.value2;
      } else {
        that.showZanTopTips(2, '消费金额须大于优惠券金额');
      }
    } else {
      if (price > coupon.value2) {
        amount = coupon.value2;
      } else {
        that.showZanTopTips(2, '商品价格没有达到优惠券使用条件');
      }
    }
    return amount;
  }

  // Utils.prototype.concessionalRate = function (that, price, coupon) {
  //   console.log(coupon)
  //   var amount = 0;
  //   if (coupon.type == "消费折扣") {
  //     if (price >= coupon.value1) {
  //       amount = price - Math.round(price * coupon.value2 / 1000)
  //     } else {
  //       that.showZanTopTips('商品价格没有达到优惠券使用条件');
  //     }
  //   } else if (coupon.type == "消费抵现") {
  //     if (price > coupon.value1) {
  //       amount = coupon.value1;
  //     } else {
  //       that.showZanTopTips('消费金额须大于优惠券金额');
  //     }
  //   } else {
  //     if (price >= coupon.value1) {
  //       amount = coupon.value2;
  //     } else {
  //       that.showZanTopTips('商品价格没有达到优惠券使用条件');
  //     }
  //   }
  //   return amount;
  // }

  /**
   * 商品优惠价计算，即减除多少钱，单位：分
   * 积分折现
   * integral：积分
   * rate折现率
   */
  Utils.prototype.integralToAmount = function (integral, rate) {
    var amount = integral / rate * 100;
    return amount;
  }

  /**
   * 退款计算,单位为分
   * total 订单总价,单位为分
   * price 被退商品价格,单位为分
   * couponPrice 优惠金额,单位为分
   */
  Utils.prototype.refund = function (total, price, couponPrice) {
    var amount = 0;
    amount = Math.round(price - (price / total) * couponPrice)
    return amount;
  }

  /**
   * 积分、优惠券同时使用的退款计算,单位为分
   * total 订单总价,单位为分
   * price 被退商品价格,单位为分
   * couponPrice 使用优惠券优惠的金额,单位为分
   * integralPrice 使用积分优惠的金额,单位为分
   */
  Utils.prototype.refund2 = function (total, price, integralPrice, couponPrice) {
    var amount = 0;
    amount = Math.round(price - (price / total) * integralPrice - (price / total) * couponPrice)
    return amount;
  }

  /**
 * 计算会员价
 * total 订单总价,单位为分
 * price 原价,单位为分
 * discount 折扣
 */
  Utils.prototype.memberPrice = function (price, discount) {
    var amount = 0;
    amount = Math.round(price * discount * 10 / 100);
    if (amount == 0) {
      return price;
    } else {
      return amount;
    }

  }

  //金额取两位小数
  Utils.prototype.toDecimal2 = function (x) {
    var f = parseFloat(x);
    if (isNaN(f)) {
      return false;
    }
    var f = Math.round(x * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
      rs = s.length;
      s += '.';
    }
    while (s.length <= rs + 2) {
      s += '0';
    }
    return s;
    // var sdata = x.toString();
    // var result = sdata.substr(0, sdata.indexOf(".") + 3);
    // return result
  }
  //数组去重
  Utils.prototype.unique2 = function (array) {
    var n = {}, r = [], len = array.length, val, type;
    for (var i = 0; i < array.length; i++) {
      val = array[i];
      type = typeof val;
      if (!n[val]) {
        n[val] = [type];
        r.push(val);
      } else if (n[val].indexOf(type) < 0) {
        n[val].push(type);
        r.push(val);
      }
    }
    return r;
  }
  return new Utils();
}


module.exports = {
  Utils: Utils
}
